package com.es.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @Author 咕唧
 * @Date 2022/6/21 17:02
 * @Version 1.0
 */
@Configuration
@ComponentScan(basePackages = "com.es")
public class EsConfig {

    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper();
    }

    @Bean
    public RestHighLevelClient client() {
        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("120.48.95.238", 9200));
        //设置超时时间
        restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
                //设置超时connect连接时间
                builder.setConnectTimeout(10000);
                //设置Socket超时时间
                builder.setSocketTimeout(10000);
                //设置request请求时间
                builder.setConnectionRequestTimeout(10000);
                return builder;
            }
        });
        restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                //设置连接总数
                httpAsyncClientBuilder.setMaxConnTotal(1000);
                //设置连接单个数量
                httpAsyncClientBuilder.setMaxConnPerRoute(100);
                return httpAsyncClientBuilder;
            }
        });

        return new RestHighLevelClient(restClientBuilder);
    }

}
